<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Form_ValidateProductBuy{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $edit = false, $data = null){
        
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validateName = new Zend_Validate();
        $validateNet = new Zend_Validate();
        $validateType = new Zend_Validate();
        $validatePrice = new Zend_Validate();
        
        $option = array('table' => 'products', 'field' => 'id');
        
        $validateName->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 150)), true);
        
        $validateNet->addValidator(new Zend_Validate_StringLength(array('max' => 50)), true);
        
        $validateType->addValidator(new Zend_Validate_StringLength(array('max' => 50)), true);
        
        $validatePrice->addValidator(new Zend_Validate_Digits(), true);
        
        /*
        * Check Name
        */
        
        if(!$validateName->isValid($params['name'])){
            $this->_messageError['name'] = '<label class="control-label" for="inputError">' . $translate->_('product name') . current($validateName->getMessages()) . '</label>';
            $this->_arrData['name'] = '';
            $this->_classError['name'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['name'] = $data['name'];
            }
        }else{
            $this->_messageError['name'] = '';
            $this->_arrData['name'] = $params['name'];
            $this->_classError['name'] = '';
        }
        
        /*
        * Check Net
        */
        
        if(!$validateNet->isValid($params['net'])){
            $this->_messageError['net'] = '<label class="control-label" for="inputError">' . $translate->_('net') . current($validateNet->getMessages()) . '</label>';
            $this->_arrData['net'] = '';
            $this->_classError['net'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['net'] = $data['net'];
            }
        }else{
            $this->_messageError['net'] = '';
            $this->_arrData['net'] = $params['net'];
            $this->_classError['net'] = '';
        }
        
        /*
        * Check Type
        */
        
        if(!$validateType->isValid($params['type'])){
            $this->_messageError['type'] = '<label class="control-label" for="inputError">' . $translate->_('type') . current($validateNet->getMessages()) . '</label>';
            $this->_arrData['type'] = '';
            $this->_classError['type'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['type'] = $data['type'];
            }
        }else{
            $this->_messageError['type'] = '';
            $this->_arrData['type'] = $params['type'];
            $this->_classError['type'] = '';
        }
        
        /*
        * Check Price
        */
        
        if(!$validatePrice->isValid($params['price'])){
            $this->_messageError['price'] = '<label class="control-label" for="inputError">' . $translate->_('price') . current($validatePrice->getMessages()) . '</label>';
            $this->_arrData['price'] = '';
            $this->_classError['price'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['price'] = $data['price'];
            }
        }else{
            $this->_messageError['price'] = '';
            $this->_arrData['price'] = $params['price'];
            $this->_classError['price'] = '';
        }
        
        if($edit){
            $this->_arrData['id'] = $data['id'];
        }
        
        if(isset($params['showinsale'])){
            $showinsale = 1;
        }else{
            $showinsale = 0;
        }
        
        if(isset($params['showallcustomer'])){
            $showallcustomer = 1;
        }else{
            $showallcustomer = 0;
        }
        
        $this->_arrData['supplier'] = $params['supplier'];
        $this->_arrData['brand'] = $params['brand'];
        $this->_arrData['showinsale'] = $showinsale;
        $this->_arrData['showallcustomer'] = $showallcustomer;
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}